<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>个人电子会员卡</title>

<script type="text/javascript" src="../resources/js/zepto1.1.6.min.js"></script>
<script src="js/code128/code128.js" type="text/javascript"
	charset="utf-8"></script>
<script src="js/code128/NewCODE128.js"></script>
<script src="js/code128/JsBarcode.js"></script>
<script type="text/javascript">
	window.onload = function() {
		var GetData = window.location.search.substr(1).split("=");
		var postData = "";
		if (GetData[0] == "barcode") {
			$.ajax({
				type : "GET",
				url : "../phones/SEND_PHONE_FOR_SCAN",
				success : function(data) {
					// 有code，说明没 登陆 或者 session 失效。   或者没权限
					if (data.code != null) {
						alert(date.info);
						return;
					}

					JsBarcode(document.getElementById("rotImg"), data, {
						quite : 10,
						format : "CODE128",
						displayValue : true,
						font : "monospace",
						textAlign : "center",
						fontSize : 12,
						backgroundColor : "",
						lineColor : "#000"
					});
					//createBarcode('div128', data, 'B');
				},
				error : function(data) {
				}
			});
		} else if (GetData[0] == "code") {
			postData = "code=" + GetData[1];
			$.ajax({
				type : "GET",
				url : "../weixin/barcode?" + postData,
				success : function(data) {
					if (data.code == 1) {
						//生成条形码
						JsBarcode(document.getElementById("rotImg"),
								data.object, {
									quite : 10,
									format : "CODE128",
									displayValue : true,
									font : "monospace",
									textAlign : "center",
									fontSize : 12,
									backgroundColor : "",
									lineColor : "#000"
								});
						//createBarcode('div128', data.object, 'B');
					} else if (data.code == 0) {
						//
						window.location.href = "wechat-login.html";
					}
				},
				error : function(data) {
				}
			});
		}
	 
		
		var oldh = document.getElementById("rotImg").height;
		var oldw = document.getElementById("rotImg").width;
		

		var param = {
			right : document.getElementById("rotRight"),
			left : document.getElementById("rotLeft"),
			img : document.getElementById("rotImg"),
			cv : document.getElementById("canvas"),
			rot : 0
		};
		 
		var rotate = function(canvas, img, rot, isH) {
			//获取图片的高宽
			var w = img.width;
			var h = img.height;
		 
			if (!isH) {
//				oldh = h;
//				oldw = w;
				var wh = oldw / oldh;
				h = window.screen.availHeight;
				w = h * wh;
			} else {
				w = oldw;
				h = oldh;
				 
			}

			//角度转为弧度
			if (!rot) {
				rot = 0;
			}
			var rotation = Math.PI * rot / 180;
			var c = Math.round(Math.cos(rotation) * 1000) / 1000;
			var s = Math.round(Math.sin(rotation) * 1000) / 1000;
			//旋转后canvas标签的大小
			canvas.height = Math.abs(c * h) + Math.abs(s * w);
			canvas.width = Math.abs(c * w) + Math.abs(s * h);
			//绘图开始
			var context = canvas.getContext("2d");
			context.save();
			//改变中心点
			if (rotation <= Math.PI / 2) {
				context.translate(s * h, 0);
			} else if (rotation <= Math.PI) {
				context.translate(canvas.width, -c * h);
			} else if (rotation <= 1.5 * Math.PI) {
				context.translate(-c * w, canvas.height);
			} else {
				context.translate(0, -s * w);
			}
			//旋转90°
			context.rotate(rotation);
			//绘制
			context.drawImage(img, 0, 0, w, h);
			context.restore();
			img.style.display = "none";
		}
		var fun = {
			right : function() {
				param.rot += 90;
				rotate(param.cv, param.img, param.rot, true);
				if (param.rot === 270) {
					param.rot = -90;
				}
			},
			left : function() {
				param.rot -= 90;
				if (param.rot === -90) {
					param.rot = 270;
				}
				rotate(param.cv, param.img, param.rot, false);
			}
		};
	 
		var global= false;
		/* param.img.onclick = function(){
			 
			 if(!global) {// 向左转
				 fun.left();
			 
			 }else{// 恢复
				 fun.right();
			 }
			 global=!global;
			return false;
		};
		param.cv.onclick=function(){
			 if(!global) {// 向左转
				 fun.left();
			 
			 }else{// 恢复
				 fun.right();
			 }
			 global=!global;
			return false;
		} */
		
	};
</script>
</head>
<body>
	<div>
		<div>
			<img id="rotImg" style="width: 100%;"  />
			<canvas id="canvas"></canvas>

		</div>
	</div>

</body>
</html>

